<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use Oss\OssClient;

require "./extend/OSS/OssClient.php";
// 应用公共文件
/**
 * 月初月末
 */
function getthemonth($date)
{
    $firstday = date('Y-m-01', strtotime($date));
    $lastday = date('Y-m-t', strtotime($date));
    return array($firstday,$lastday);
}

/**
 * @param $birthday
 * @return 年龄
 */
function getAge($birthday){
    $age = date('Y', time()) - date('Y', strtotime($birthday)) - 1;
    if (date('m', time()) == date('m', strtotime($birthday))){

        if (date('d', time()) > date('d', strtotime($birthday))){

            $age++;
        }else{
            $age =0;
        }
    }elseif (date('m', time()) > date('m', strtotime($birthday))){
        $age++;
    }
    return $age;
}
/**
 * 将时间戳转换为日期时间
 * @param int $time 时间戳
 * @param string $format 日期时间格式
 * @return string
 */
function datetime($time, $format = 'Y-m-d H:i:s')
{
    $time = is_numeric($time) ? $time : strtotime($time);
    return date($format, $time);
}

/**
 * @param $sex
 * @return 获取性别对应的数字
 */
function get_sex_num($sex){
    if($sex=='男'){
        return 1;
    }else{
        return 2;
    }
}


/**
 * @param $sex
 * @return 获取性别对应的文字
 */
function get_sex_str($sex){
    if($sex==1){
        return '男';
    }else{
        return '女';
    }
}

header('Access-Control-Allow-Origin: *');
// 上传OSS图片
function uploadimg($filename,$type)
{


    $ossClient = new OssClient();
    $result = $ossClient->uploadFile($filename,$type);
    if($type!='')
        if($result["info"]['http_code'] == '200'){
            $return_url = $result["info"]["url"];
            $url = str_replace("vpc100-","",$return_url);
            //print_r($url);
            $reinfo['error'] = 0;
            $reinfo['url'] = $url;

        }else{
            $url = '';
            $reinfo['error'] = $result["info"]['http_code'];
            $reinfo['url'] = $url;
        }
    // $url = $result["info"]["url"];
    return $reinfo;
}

function uploadimgToLocal($filename)
{

    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file($filename);

    // 移动到框架应用根目录/public/uploads/ 目录下
    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
    if($info){
        $reinfo = [];
        // 成功上传后 获取上传信息
        $reinfo['error'] = 0;
        $domain = 'http://' . $_SERVER['HTTP_HOST'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
        $reinfo['url'] = $domain."/public/uploads/".$info->getSaveName();
        return $reinfo;
    }else{
        // 上传失败获取错误信息
        $url = '';
        $reinfo['error'] = $file->getError();
        $reinfo['url'] = $url;
        return $reinfo;
    }
    // return $url;
}


// cURL函数简单封装
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}


function is_idcard( $id )
{
    $id = strtoupper($id);
    $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
    $arr_split = array();
    if(!preg_match($regx, $id))
    {
        return FALSE;
    }
    if(15==strlen($id)) //检查15位
    {
        $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";

        @preg_match($regx, $id, $arr_split);
        //检查生日日期是否正确
        $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
        if(!strtotime($dtm_birth))
        {
            return FALSE;
        } else {
            return TRUE;
        }
    }
    else      //检查18位
    {
        $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
        @preg_match($regx, $id, $arr_split);
        $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
        if(!strtotime($dtm_birth)) //检查生日日期是否正确
        {
            return FALSE;
        }
        else
        {
            //检验18位身份证的校验码是否正确。
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            $sign = 0;
            for ( $i = 0; $i < 17; $i++ )
            {
                $b = (int) $id{$i};
                $w = $arr_int[$i];
                $sign += $b * $w;
            }
            $n = $sign % 11;
            $val_num = $arr_ch[$n];
            if ($val_num != substr($id,17, 1))
            {
                return FALSE;
            } //phpfensi.com
            else
            {
                return TRUE;
            }
        }
    }

}
